//
//  MCTrainDetailCell.swift
//  MCBodyBuildingPro
//
//  Created by Renlvshijie on 2023/5/31.
//

import UIKit
import SnapKit
import QMUIKit
import Kingfisher


class MCTrainDetailCell: UITableViewCell {

    @IBOutlet weak var trainCollectionView: UICollectionView!
    
    private let mcTrainDetailCollectionIdentifier = "mcTrainDetailCollectionIdentifier"
    
    private let mcTrainDetailResusableHeaderIdentifier = "mcTrainDetailResusableHeaderIdentifier"
    
    private let mcTrainDetailResusableFooterIdentifier = "mcTrainDetailResusableFooterIdentifier"
    
    var actionPlanDetailActionListModelInfo:Array<MCTrainDetailActionPlanDetailActionList>?
    
    var actionPlanListModel:MCTrainDetailActionPlanDetailList?
    
    var headerSection:Int = 1
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
        
        let trainCollectionLayout = UICollectionViewFlowLayout.init()
        self.trainCollectionView.dataSource = self
        self.trainCollectionView.delegate = self
        self.trainCollectionView.setCollectionViewLayout(trainCollectionLayout, animated: true)
        self.trainCollectionView.isScrollEnabled = false
        self.trainCollectionView.register(UINib.init(nibName: "MCTrainDetailCollectionCell", bundle: Bundle.main), forCellWithReuseIdentifier: mcTrainDetailCollectionIdentifier)
        
        self.trainCollectionView.register(UINib.init(nibName:"MCTrainHeaderReusableView", bundle: Bundle.main), forSupplementaryViewOfKind:UICollectionView.elementKindSectionHeader, withReuseIdentifier: mcTrainDetailResusableHeaderIdentifier)
        
        self.trainCollectionView.register(UINib(nibName:"MCTrainFooterReusableView", bundle: Bundle.main), forSupplementaryViewOfKind:UICollectionView.elementKindSectionFooter, withReuseIdentifier: mcTrainDetailResusableFooterIdentifier)
        
        
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
    }
    
    
}

extension MCTrainDetailCell:UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout {
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize(width: 120, height: 90)
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        return 10
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
        return 10
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
        return UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10)
    }
    
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return self.actionPlanDetailActionListModelInfo?.count ?? 0
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell:MCTrainDetailCollectionCell = collectionView.dequeueReusableCell(withReuseIdentifier: mcTrainDetailCollectionIdentifier, for: indexPath) as! MCTrainDetailCollectionCell
        let acitonInfo = self.actionPlanDetailActionListModelInfo?[indexPath.row]
        cell.headerImageView.kf.setImage(with: URL(string: acitonInfo?.previewUrl ?? ""))
        cell.titleInfoLabel.text = acitonInfo?.actionName ?? ""
        cell.timeInfoLabel.text = "\(String(describing: (acitonInfo?.actionTime)!))S"
        
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
        return CGSize(width: 200, height: 30)
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -> CGSize {
        return CGSize(width: 200, height: 30)
    }
    
    func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
        
        var reusableView:UICollectionReusableView!
        
        if kind == UICollectionView.elementKindSectionHeader {
            let headerReusableView:MCTrainHeaderReusableView = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: mcTrainDetailResusableHeaderIdentifier, for: indexPath) as! MCTrainHeaderReusableView
            
            headerReusableView.titleHeaderLabel.text  = "第\(headerSection)组"
            
            reusableView = headerReusableView
            
        }
        
        if kind == UICollectionView.elementKindSectionFooter {
            let footerReusableView:MCTrainFooterReusableView = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionView.elementKindSectionFooter, withReuseIdentifier: mcTrainDetailResusableFooterIdentifier, for: indexPath) as! MCTrainFooterReusableView
             reusableView = footerReusableView
            
            footerReusableView.timeXiuLabel.text = "\(String(describing: (actionPlanListModel?.breakTime)!))S"
            
        }
        
        return reusableView
    }
    
    
}
